﻿
#region Get-PageFile

function Get-PageFile
{

    <#
        .Synopsis 
            Gets the Page File info for specified host
            
        .Description
            Gets the Page File info for specified host
            
        .Parameter ComputerName
            Name of the Computer to get the Paging File info from (Default is localhost.)
            
        .Example
            Get-PageFile
            Description
            -----------
            Gets Page File from local machine
    
        .Example
            Get-PageFile -ComputerName MyServer
            Description
            -----------
            Gets Page File from MyServer
            
        .Example
            $Servers | Get-PageFile
            Description
            -----------
            Gets Page File for each machine in the pipeline
            
        .OUTPUTS
            PSObject
            
        .Notes
            NAME:      Get-PageFile 
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            #Requires -Version 2.0
    #>
    
    [Cmdletbinding()]
    Param(
        [alias('dnsHostName')]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:COMPUTERNAME
    )
    
    Process 
    {
        Write-Verbose " [Get-PageFile] :: Process Start"
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
        }
        if(Test-Host $ComputerName -TCPPort 135)
        {
            try
            {
                Write-Verbose " [Get-PageFile] :: Collecting Paging File Info"
                $PagingFiles = Get-WmiObject Win32_PageFile -ComputerName $ComputerName -ErrorAction SilentlyContinue
                if($PagingFiles)
                {
                    foreach($PageFile in $PagingFiles)
                    {
                        $myobj = @{
                            ComputerName = $ComputerName
                            Name         = $PageFile.Name
                            SizeGB       = [int]($PageFile.FileSize / 1GB)
                            InitialSize  = $PageFile.InitialSize
                            MaximumSize  = $PageFile.MaximumSize
                        }
                        
                        $obj = New-Object PSObject -Property $myobj
                        $obj.PSTypeNames.Clear()
                        $obj.PSTypeNames.Add('BSonPosh.Computer.PageFile')
                        $obj
                    }
                }
                else
                {
                    $Pagefile = Get-ChildItem \\$ComputerName\c$\pagefile.sys -Force -ErrorAction SilentlyContinue 
                    if($PageFile)
                    {
                        $myobj = @{
                            ComputerName = $ComputerName
                            Name         = $PageFile.Name
                            SizeGB       = [int]($Pagefile.Length / 1GB)
                            InitialSize  = "System Managed"
                            MaximumSize  = "System Managed"
                        }
                        
                        $obj = New-Object PSObject -Property $myobj
                        $obj.PSTypeNames.Clear()
                        $obj.PSTypeNames.Add('BSonPosh.Computer.PageFile')
                        $obj
                    }
                    else
                    {
                        Write-Host "[Get-PageFile] :: No Paging File setting found. Most likely set to system managed or you do not have access."
                    }
                }
            }
            catch
            {
                 Write-Verbose " [Get-PageFile] :: [$ComputerName] Failed with Error: $($Error[0])" 
            }
        }
    }        
}

#endregion 


#region Get-PageFileSetting

function Get-PageFileSetting
{

    <#
        .Synopsis 
            Gets the Page File setting info for specified host
            
        .Description
            Gets the Page File setting info for specified host
            
        .Parameter ComputerName
            Name of the Computer to get the Page File setting info from (Default is localhost.)
            
        .Example
            Get-PageFileSetting
            Description
            -----------
            Gets Page File setting from local machine
    
        .Example
            Get-PageFileSetting -ComputerName MyServer
            Description
            -----------
            Gets Page File setting from MyServer
            
        .Example
            $Servers | Get-PageFileSetting
            Description
            -----------
            Gets Page File setting for each machine in the pipeline
            
        .OUTPUTS
            PSObject
            
        .Notes
            NAME:      Get-PageFileSetting 
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            #Requires -Version 2.0
    #>
    
    [Cmdletbinding()]
    Param(
        [alias('dnsHostName')]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:COMPUTERNAME
    )
    
    Process 
    {
        Write-Verbose " [Get-PageFileSetting] :: Process Start"
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
        }
        if(Test-Host $ComputerName -TCPPort 135)
        {
            try
            {
                Write-Verbose " [Get-PageFileSetting] :: Collecting Paging File Info"
                $PagingFiles = Get-WmiObject Win32_PageFileSetting -ComputerName $ComputerName -EnableAllPrivileges
                if($PagingFiles)
                {
                    foreach($PageFile in $PagingFiles)
                    {
                        $PageFile 
                    }
                }
                else
                {
                    Write-Host "No Paging File setting found. Most likely set to system managed"
                }
            }
            catch
            {
                 Write-Verbose " [Get-PageFileSetting] :: [$ComputerName] Failed with Error: $($Error[0])" 
            }
        }
    }        
}

#endregion 


#region Set-PageFileSetting

function Set-PageFileSetting
{

    <#
        .Synopsis 
            Sets the Paging File info for specified host
            
        .Description
            Sets the Paging File info for specified host
            
        .Parameter ComputerName
            Name of the Computer to set the Paging File info from (Default is localhost.)
        
        .Parameter InitialSize
            Initial size of the page file in MB. For example if you want 8GB use 8192.
            
        .Parameter MaximumSize
            Maximum size of the page file in MB. For example if you want 8GB use 8192.
            
        .Example
            Set-PageFileSetting -InitialSize 4096 -MaximumSize 4096
            Description
            -----------
            Sets Paging File on local machine
    
        .Example
            Set-PageFileSetting -InitialSize 4096 -MaximumSize 4096 -ComputerName MyServer
            Description
            -----------
            Sets Paging File from MyServer
            
        .Example
            $Servers | Set-PageFileSetting -InitialSize 4096 -MaximumSize 4096
            Description
            -----------
            Sets Paging File for each machine in the pipeline
            
        .OUTPUTS
            PSObject
            
        .Notes
            NAME:      Set-PageFileSetting 
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            #Requires -Version 2.0
    #>

    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
    
        [Alias('dnsHostName')]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:COMPUTERNAME,
        
        [Parameter(Mandatory=$true)]
        [int]$InitialSize,
        
        [Parameter(Mandatory=$true)]
        [int]$MaximumSize

    )
    
    Process 
    {
        Write-Verbose " [Set-PageFileSetting] :: Process Start"
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
            
        }
        if(Test-Host $ComputerName -TCPPort 135)
        {
            try
            {
                Write-Verbose " [Set-PageFileSetting] :: Collecting Paging File Info"
                $PageFiles = Get-PageFileSetting -ComputerName $ComputerName
                foreach($PageFile in $PageFiles)
                {
                    Write-Verbose " [Set-PageFileSetting] :: Setting InitialSize to [$InitialSize]"
                    $PageFile.InitialSize = $InitialSize
                    
                    Write-Verbose " [Set-PageFileSetting] :: Setting $MaximumSize to [$MaximumSize]"
                    $PageFile.MaximumSize = $MaximumSize
                    
                    Write-Verbose " [Set-PageFileSetting] :: Commiting Data"
                    $PageFile.Put() | Out-Null
                    
                    Get-PageFileSetting $ComputerName
                }
                
            }
            catch
            {
                 Write-Verbose " [Set-PageFileSetting] :: [$ComputerName] Failed with Error: $($Error[0])" 
            }
        }
    }        
}

#endregion 


#region Disable-PagingFileAutoManage 

function Disable-PagingFileAutoManage
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
    
        [Alias('dnsHostName')]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:COMPUTERNAME
        
    )
    Process
    {
        Write-Verbose " [Disable-PagingFileAutoManage] :: Getting ComputerSystem Object for [$ComputerName]"
        $CompWMI = Get-WmiObject Win32_ComputerSystem -ComputerName $ComputerName -EnableAllPrivileges
        
        Write-Verbose " [Disable-PagingFileAutoManage] :: Setting AutomaticManagedPagefile to $False"
        $CompWMI.AutomaticManagedPagefile = $false
        try
        {
            $CompWMI.Put() 
        }
        catch
        {
            
        }
        
        Write-Verbose " [Disable-PagingFileAutoManage] :: Gettign PageFile Config"
        Get-PageFileSetting -ComputerName $ComputerName
    }
}

#endregion 


#region Enable-PagingFileAutoManage 

function Enable-PagingFileAutoManage
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
    
        [Alias('dnsHostName')]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:COMPUTERNAME
        
    )
    Process
    {
        Write-Verbose " [Enable-PagingFileAutoManage] :: Getting ComputerSystem Object for [$ComputerName]"
        $CompWMI = Get-WmiObject Win32_ComputerSystem -ComputerName $ComputerName -EnableAllPrivileges
        
        Write-Verbose " [Enable-PagingFileAutoManage] :: Setting AutomaticManagedPagefile to $True"
        $CompWMI.AutomaticManagedPagefile = $True
        try
        {
            $CompWMI.Put() 
        }
        catch
        {
            
        }
        
        Write-Verbose " [Enable-PagingFileAutoManage] :: Getting PageFile Config"
        Get-PageFileSetting -ComputerName $ComputerName
    }   
}

#endregion 